//
//  StatusPictureView.swift
//  MySinaWeiBo
//
//  Created by zhangfuwei on 15/8/6.
//  Copyright © 2015年 zhangfuwei. All rights reserved.
//

import UIKit
import SDWebImage

class StatusPictureView: UICollectionView {

    var status:Status? {
        didSet {
            sizeToFit()

            reloadData()
        }
    }
    
    override func sizeThatFits(size: CGSize) -> CGSize {
//        super.sizeThatFits(size)
        
       return calculateSize()
    }
    
    //  计算 size
    func calculateSize() ->CGSize{
        //  设置默认的 itemSize 大小
        let itemSize = CGSize(width: 90, height: 90)
        pictureLayout.itemSize = itemSize
        
        let cellMargin:CGFloat = 10
        //  每行最多3列图片
        let col = 3
        
         let count = status?.pictureURLs?.count ?? 0

        if count == 0 {
            return CGSizeZero
        }
        
        if count == 1 {
            var size = CGSize(width: 150, height: 120)
            
            ///  将一张的图片缓存下来
            //  获取绝对路径
            let key = status!.pictureURLs![0].absoluteString
            
            //  image 可能由于其他因素可能不会被缓存下来
             let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(key)
            //  如果图片存在
            if image != nil {
                size = image.size
            }
            ///  进行设置,防止一张图片过大或者过小
            size.width = size.width < 40 ? 40 : size.width
            size.width = size.width > UIScreen.mainScreen().bounds.width ? 150 : size.width
            pictureLayout.itemSize = size

            return size
        }
        
        if count == 4 {
            let w = itemSize.width * 2 + cellMargin
            
            return CGSize(width: w, height: w)
        }
        
        //  除去上述几种情况之外的
        //  计算行数
        let row = (count - 1) / col + 1
        
        let w = itemSize.width * CGFloat(col)  + cellMargin * CGFloat(col - 1)
        let h = itemSize.height * CGFloat(row) + cellMargin * CGFloat(row - 1)
        return CGSize(width: w, height: h  )
        
    }
    
    
    let pictureLayout =  UICollectionViewFlowLayout()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: pictureLayout)
        backgroundColor = UIColor.lightGrayColor()
        
        registerClass(StatusPictureViewCell.self, forCellWithReuseIdentifier: "FWStatusPictureView")
        self.dataSource = self
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}


// MARK: 定义一个数据源方法扩展类

extension StatusPictureView: UICollectionViewDataSource {
    
    // MARK:  数据源方法
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return status!.pictureURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("FWStatusPictureView", forIndexPath: indexPath) as! StatusPictureViewCell
        //  设置 cell 的内容
        cell.picURL = status!.pictureURLs![indexPath.item]
        
        return cell
    }

}

// MARK:  重新定义一个 cell 类
class StatusPictureViewCell: UICollectionViewCell {
    
    var picURL:NSURL? {
        didSet {
            
            iconV.sd_setImageWithURL(picURL)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //  tj  控件
        contentView.addSubview(iconV)
        
        //  sz  约束
        iconV.ff_Fill(contentView)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var iconV:UIImageView = {
        let iv = UIImageView()
        return iv
        
    }()
    
}

